package com.fitbet.admin.service.bonus.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.ClassUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.support.QueryFilter;
import com.fitbet.admin.dao.UserWithdrawalDao;
import com.fitbet.admin.entity.search.bonus.UserWithdrawalSearch;
import com.fitbet.admin.service.bonus.WithdrawalAuditingsService;
import com.fitbet.admin.service.impl.DataServiceImpl;
import com.fitbet.entity.user.UserWithdrawal;

/**
 * @author xiaoHU
 *
 */
@Service("betBonusSupportService")
public class WithdrawalAuditingsServiceImpl extends DataServiceImpl<UserWithdrawal> implements WithdrawalAuditingsService {

    @Dao
    private UserWithdrawalDao userWithdrawalDao;
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    /* (non-Javadoc)
     * @see com.fitbet.admin.service.bonus.BetBonusSupportService#pay(com.fitbet.entity.user.UserWithdrawal)
     */
    public void pay(UserWithdrawal userWithdrawal) {
        
        genericDaoSupport.update(userWithdrawal.paid());
        
    }

    /* (non-Javadoc)
     * @see com.fitbet.admin.service.bonus.BetBonusSupportService#count()
     */
    public int count() {
        return userWithdrawalDao.count();
    }

    /* (non-Javadoc)
     * @see com.fitbet.admin.service.bonus.BetBonusSupportService#list(com.demo2do.core.entity.page.Page)
     */
    public List<UserWithdrawal> list(Page page) {
        List<UserWithdrawal> userWithdrawals = userWithdrawalDao.list(page.getBeginIndex(), page.getMaxResultRecords());
        page.calculate();
        return userWithdrawals;
    }

    /* (non-Javadoc)
     * @see com.fitbet.admin.service.bonus.BetBonusSupportService#countSearch(java.lang.Class, com.demo2do.core.persistence.support.QueryFilter)
     */
    public int countSearch(Class<UserWithdrawal> clazz, QueryFilter queryFilter) {
        return genericDaoSupport.searchForInt("SELECT COUNT(*) FROM " + ClassUtils.getShortClassName(clazz) + " WHERE disabled = false AND paid = false" + queryFilter.clauseSentence(), queryFilter.parameters());
    }

    /* (non-Javadoc)
     * @see com.fitbet.admin.service.bonus.BetBonusSupportService#listSearch(com.fitbet.admin.entity.search.bonus.UserWithdrawalSearch, com.demo2do.core.entity.page.Page)
     */
    @SuppressWarnings("unchecked")
    public List<UserWithdrawal> listSearch(UserWithdrawalSearch search, Page page) {
        search.getPreparedFilter(false).addEquals("paid", false);
        String sentence = search.buildSentenceTemplate();
        Map<String, Object> params = search.buildParameters();
        List<UserWithdrawal> result = genericDaoSupport.searchForList(sentence, params, page.getBeginIndex(), page.getEveryPage());
        page.calculate();
        return result;
    }

}
